package org.springblade.pm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.pm.entity.Plan;
import org.springblade.pm.entity.PlanDetails;
import org.springblade.pm.entity.PlanMilestone;
import org.springblade.pm.entity.XmglProject;
import org.springblade.pm.service.IOverViewService;
import org.springblade.pm.service.IPlanService;
import org.springblade.pm.service.IXmglProjectService;
import org.springblade.pm.vo.XmglProjectVo;
import org.springblade.pm.wrappper.XmglProjectWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("overView")
@AllArgsConstructor
public class OverViewController {

	private IXmglProjectService xmglProjectService;
	private IOverViewService overViewService;
	private IPlanService planService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情")
	public R<XmglProjectVo> detail(Integer projectId) {

		XmglProject detail = xmglProjectService.getById(projectId);
		XmglProjectVo xmglProjectVo = XmglProjectWrapper.build().entityVO(detail);

		return R.data(XmglProjectWrapper.build().entityVO(detail));
	}

	/**
	 *项目人员人力资源统计列表
	 */
	@GetMapping("/getManpower")
	@ApiOperation(value = "获取人力资源信息")
	public R getManpower(String projectCode) throws ParseException {
		HashMap<String, Object> dataMap = new HashMap<>();
		Map<String, Object> humanTaskTime = overViewService.getHumanTaskTime(projectCode);
		String maxMonth = humanTaskTime.get("maxMonth").toString();
		String minMonth = humanTaskTime.get("minMonth").toString();

		if ("0".equals(maxMonth)||"0".equals(minMonth)){
			dataMap.put("series",null);
			dataMap.put("dateList",null);
			return R.data(dataMap);
		}
		SimpleDateFormat sdfy = new SimpleDateFormat("yyyy-MM");
		Date maxTime = sdfy.parse(maxMonth);
		Date minTime = sdfy.parse(minMonth);

		Calendar c = Calendar.getInstance();
		c.setTime(minTime);
		ArrayList<String> dateList = new ArrayList<>();
		while (c.getTimeInMillis()<maxTime.getTime()){
			dateList.add(sdfy.format(c.getTime()));
			c.add(Calendar.MONTH, 1);//+1月
		}
		Map<String, Object> params = new HashMap<>();
		List<String> users = overViewService.getUserByProject(projectCode);
		ArrayList<Object> series = new ArrayList<>();
		for (String user:users){
			ArrayList<String> list = new ArrayList<>();
			for (String date:dateList){
				params.put("projectCode",projectCode);
				params.put("date",date);
				params.put("user",user);
				list.add(overViewService.getUserFreeRank(params));
			}
			HashMap<String, Object> map = new HashMap<>();
			map.put("data",list);
			map.put("name",user);
			series.add(map);
		}

		dataMap.put("series",series);
		dataMap.put("dateList",dateList);
		return R.data(dataMap);
	}


	/**
	 * 详情
	 */
	@GetMapping("/getActiveList")
	@ApiOperation(value = "获取活动信息")
	public R getActiveList(Integer projectId) {
		ArrayList<Map<String, String>> dataList = new ArrayList<>();
		List<Map<String, String>> activeList = overViewService.getActiveList(projectId);
		for (Map<String,String> map:activeList){
			Map<String, String> dataMap = new HashMap<>();
			dataMap.put("startTime",map.get("startTime"));
			dataMap.put("actvieName",map.get("activeName"));
			dataList.add(dataMap);
		}
		return R.data(dataList);
	}

	@GetMapping("/getPlanDetails")
	@ApiOperation(value = "获取计划信息")
	public R getPlanDetails(Integer projectId) {
		HashMap<String, Object> dataMap = new HashMap<>();
		Plan plan =null;
		QueryWrapper<Plan> wrapper = new QueryWrapper<>();
		wrapper.eq("pid",projectId).select("version").orderByDesc("version");
		List<Object> list = planService.listObjs(wrapper);
		for (int i = 0; i <list.size(); i++) {
			list.set(i,list.get(i).toString());
		}
		if(list.size()<1){
			dataMap.put("planMap",null);
			dataMap.put("lifeCycleList",null);
			return R.data(dataMap);
		}else {
			plan =  planService.selectPlan(projectId, list.get(0).toString());
		}

		Integer stageLastTime=null;
		StringBuffer stageLastweek=new StringBuffer("");
		ArrayList<Date> dateList = new ArrayList<>();
		ArrayList<Map<String,String>> lifeCycleList = new ArrayList<>();
		SimpleDateFormat sdfy = new SimpleDateFormat("yyyy-MM-dd");
		for (PlanDetails planDetails:plan.getPdList()){
			dateList.add(planDetails.getPlanstarttime());
			dateList.add(planDetails.getPlanendtime());
			if(planDetails.getPlanstarttime().getTime()<System.currentTimeMillis()&&System.currentTimeMillis()<planDetails.getPlanendtime().getTime()) {
				stageLastTime = (int) ((planDetails.getPlanendtime().getTime() - System.currentTimeMillis()) / (1000 * 3600 * 24)+1);
				for (PlanMilestone planMilestone : planDetails.getChildren()) {
					if (planMilestone.getPlanstarttime().getTime()>System.currentTimeMillis()){
						stageLastweek.append(","+planMilestone.getName());
					}
				}
			}
			HashMap<String, String> map = new HashMap<>();
			map.put("name",planDetails.getName());
			map.put("value",sdfy.format(planDetails.getPlanstarttime()));
			lifeCycleList.add(map);
		}
		HashMap<String, Object> planMap = new HashMap<>();
		Date max = Collections.max(dateList);
		Date min = Collections.min(dateList);
		int planWeekSum = (int)((max.getTime() - min.getTime()) / (1000 * 3600 * 24)+1);
		planMap.put("planWeekSum",planWeekSum);
		int expendWeek = (int)((System.currentTimeMillis() - min.getTime()) / (1000 * 3600 * 24)+1);
		if (expendWeek<0){
			planMap.put("expendWeek",0);
		}else {
			planMap.put("expendWeek",expendWeek);
		}
		int planLastWeek =(int)((max.getTime() - System.currentTimeMillis()) / (1000 * 3600 * 24)+1);
		planMap.put("planLastWeek",planLastWeek);
		planMap.put("stageLastWeek",stageLastTime);
		planMap.put("stageLastWork",stageLastweek.toString());


		dataMap.put("planMap",planMap);
		dataMap.put("lifeCycleList",lifeCycleList);
		return R.data(dataMap);
	}

	/*public Plan getPlan(Integer projectId) {
		QueryWrapper<Plan> wrapper = new QueryWrapper<>();
		wrapper.eq("pid",projectId).select("version").orderByDesc("version");
		List<Object> list = planService.listObjs(wrapper);
		for (int i = 0; i <list.size(); i++) {
			list.set(i,list.get(i).toString());
		}
		if(list.size()<1){
			return null;
		}else {
			return planService.selectPlan(projectId, list.get(0).toString());
		}
	}
*/
}
